package it.uniroma3.dia.ia.textcategorization.categorizer.opennlp;

import java.util.HashSet;
import java.util.Set;
import opennlp.tools.doccat.DocumentCategorizer;
import opennlp.tools.doccat.DocumentCategorizerME;
import it.uniroma3.dia.ia.textcategorization.categorizer.Categorizer;
import it.uniroma3.dia.ia.textcategorization.model.Category;
import it.uniroma3.dia.ia.textcategorization.model.Text;

public final class OpenNLPCategorizer implements Categorizer {
	
	private Set<Category> categories;
	private DocumentCategorizer categorizer;
	
	OpenNLPCategorizer(Set<Category> categories, DocumentCategorizerME categorizer) {
		this.categories = new HashSet<Category>(categories);
		this.categorizer = categorizer;
	}

	@Override
	public Set<Category> getCategories() {
		return this.categories;
	}

	@Override
	public Category categorize(Text text) {
		String textString = text.toString();
		double[] results = this.categorizer.categorize(textString);
		categorizer.getAllResults(results);
		String categoryString = categorizer.getBestCategory(results);
		return Category.valueOf(categoryString);
	}
	
}
